package controllers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.ning.http.client.Cookie;

import play.mvc.Controller;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import models.Pontuacao;



public class inserirPontuacao extends Application {

	private static Connection conexao;
	private static Statement comando;
	
	
	
	/***********************************************************************************/
	
	protected static void conectar() throws SQLException {  
	    
		  
		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
		}

			conexao = DriverManager.getConnection(  
			        "jdbc:postgresql://localhost:5432/SGRv2",  
			        "postgres",  
			        "senha");
		
			comando =  conexao.createStatement();

	}

    /*********************************************************************************/
	
	public static void inserirPontuacao(int idpontuacao) throws SQLException {

		String query = request.querystring.toString();
		
		int inicioPTID = query.indexOf("PTID");
		int fimPTID = query.indexOf("&RGID");	
		idpontuacao = Integer.parseInt(query.substring(inicioPTID + 5, fimPTID) );	
		
		int inicioRGID = query.indexOf("RGID");
		int fimRGID = query.indexOf("&BCID");	
		int idregata = Integer.parseInt(query.substring(inicioRGID + 5, fimRGID) );	

		int inicioBCID = query.indexOf("BCID");
		int idbarco = Integer.parseInt(query.substring(inicioBCID + 5) );	
		
		
		if(idpontuacao > 0)
		{
			conectar();	    	
	    	ResultSet reader = comando.executeQuery("select * from pontuacao where id =" + idpontuacao );	    	 
	    	Pontuacao novo = new Pontuacao();	    	
	    	novo = Pontuacao.Pontos(idpontuacao);
	    	int valor = novo.getpontuacao();
	    	
	    	render(valor, idpontuacao, idregata, idbarco );
		}
		else
		{
			String valor = "0";
			
			render(valor, idpontuacao, idregata, idbarco );
		}
	}

	/***********************************************************************************/
	
	public static void atualizar( String pontos, String hdfpontuacao, String hdfregata, String hdfbarco ) throws SQLException
	{
		
		String valor = "nome";	
				
		if(pontos.isEmpty())
		{
			valor = "A pontuação não pode ser vazia";
		}
		else
		{
			conectar();
			String idrodada = "0";
			ResultSet reader = comando.executeQuery("select idrodada from regata where id = " + hdfregata);
	    	
	    	while (reader.next())
	    	{
	    		 idrodada = ((String)reader.getString("idrodada") );
	    	}
	    		    		    	
			if(Integer.parseInt(hdfpontuacao) == 0)
			{
				comando.executeUpdate("insert into pontuacao (idbarco, pontuacao, idregata) values ("+ hdfbarco + ", " + pontos + ", " + hdfregata + ")");
			}
			else
			{
				comando.executeUpdate("update pontuacao set pontuacao = "+ pontos + " where id = " + hdfpontuacao);
			}
			
			visualizarRegatas.visualizarRegatas(Integer.parseInt(idrodada));
		}
	}
	
	/*************************************************************/

}
